/**
 * 利用x&(x-1)删除最右边的1bit位特性重写更快的1bit统计
 */

#include <stdio.h>

int bitcount(unsigned x);

int main() {
    int x = 0x5050;
    printf("1 bits: %d\n", bitcount(x));

    return 0;
}

int bitcount(unsigned x) {
    int b = 0;

    while (x) {
        x &= (x-1);
        b++;
    }

    return b;
}
